#!/usr/bin/env python3
"""竹马法考网站结构分析工具"""
import requests
from bs4 import BeautifulSoup
import json

BASE_URL = "https://www.zhumavip.com"

def analyze_login_page():
    """分析登录页面"""
    print("=" * 50)
    print("分析登录页面...")
    print("=" * 50)
    
    try:
        response = requests.get(f"{BASE_URL}/login", timeout=10)
        print(f"状态码: {response.status_code}")
        print(f"URL: {response.url}")
        
        # 查找登录表单
        soup = BeautifulSoup(response.text, 'html.parser')
        forms = soup.find_all('form')
        print(f"\n找到 {len(forms)} 个表单")
        
        for i, form in enumerate(forms):
            print(f"\n表单 {i+1}:")
            print(f"  Action: {form.get('action', 'N/A')}")
            print(f"  Method: {form.get('method', 'N/A')}")
            
            inputs = form.find_all('input')
            print(f"  输入字段:")
            for inp in inputs:
                print(f"    - {inp.get('name', 'N/A')}: {inp.get('type', 'N/A')}")
        
        # 查找可能的 API 端点
        print("\n查找可能的 API 调用...")
        script_tags = soup.find_all('script')
        for script in script_tags:
            if script.string:
                if 'api' in script.string.lower() or 'login' in script.string.lower():
                    print(f"  发现可能的 API: {script.string[:200]}...")
    
    except Exception as e:
        print(f"分析失败: {e}")


def analyze_exam_page():
    """分析真题页面"""
    print("\n" + "=" * 50)
    print("分析真题页面...")
    print("=" * 50)
    
    try:
        # 尝试访问真题页面（可能需要登录）
        response = requests.get(f"{BASE_URL}/exam", timeout=10)
        print(f"状态码: {response.status_code}")
        print(f"URL: {response.url}")
        
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # 查找年份链接
        year_links = soup.find_all('a', href=True)
        print(f"\n找到 {len(year_links)} 个链接")
        
        exam_links = [link for link in year_links if 'exam' in link.get('href', '').lower() or 'year' in link.get('href', '').lower()]
        print(f"可能的真题链接: {len(exam_links)}")
        
        for link in exam_links[:5]:  # 只显示前5个
            print(f"  - {link.get('href')}: {link.get_text(strip=True)}")
    
    except Exception as e:
        print(f"分析失败: {e}")


def suggest_api_endpoints():
    """建议可能的 API 端点"""
    print("\n" + "=" * 50)
    print("建议的 API 端点（需要验证）:")
    print("=" * 50)
    
    suggestions = [
        f"{BASE_URL}/api/user/login",
        f"{BASE_URL}/api/login",
        f"{BASE_URL}/api/auth/login",
        f"{BASE_URL}/api/exam/list",
        f"{BASE_URL}/api/exam/questions",
        f"{BASE_URL}/api/question/list",
        f"{BASE_URL}/api/v1/exam/list",
    ]
    
    for endpoint in suggestions:
        print(f"  - {endpoint}")


def main():
    print("🔍 竹马法考网站结构分析工具")
    print("\n提示：")
    print("1. 使用浏览器开发者工具（F12）查看实际网络请求")
    print("2. 登录后访问真题页面，观察 Network 标签中的请求")
    print("3. 记录登录和获取题目的 API 地址")
    print("\n开始分析...\n")
    
    analyze_login_page()
    analyze_exam_page()
    suggest_api_endpoints()
    
    print("\n" + "=" * 50)
    print("下一步操作：")
    print("=" * 50)
    print("1. 使用浏览器访问 https://www.zhumavip.com")
    print("2. 打开开发者工具（F12）-> Network 标签")
    print("3. 登录账号，观察登录请求的详细信息")
    print("4. 访问真题页面，观察数据加载请求")
    print("5. 将找到的 API 地址更新到 zhuma_scraper.py 中")


if __name__ == "__main__":
    main()

